import copy


class Common:

    def __init__(self):
        pass

    # 根据关键字提取所在行，不含匹配的字符串 前匹配
    @staticmethod
    def extract_by_key(content, key_s, key_e=''):
        return Common.get_between(content, key_s, key_e)

    # 根据关键字提取所在行，含匹配的字符串 前匹配
    @staticmethod
    def extract_by_key2(content, key_s, key_e=''):
        return Common.get_between(content, key_s, key_e, True)

    # 根据关键字提取所在行，不含匹配的字符串 后匹配
    @staticmethod
    def end_key(content, key_e, key_s=''):
        i = content.find(key_e)
        j = 0
        if key_s:
            j = content.find(key_s)
        if i != -1:  # 匹配到结束字符串
            if j != -1 and j != 0:  # 匹配到开始字符串
                return content[j:i].strip()
            else:
                return content[0:i].strip()
        else:
            return content

    # 根据关键字提取所在行，含匹配的字符串 后匹配
    @staticmethod
    def end_key2(content, key_e, key_s=''):
        i = content.find(key_e)
        j = 0
        if key_s:
            j = content.find(key_s)
        if i != -1:  # 匹配到结束字符串
            if j != -1 and j != 0:  # 匹配到开始字符串
                return content[j:i].strip()
            else:
                return content[0:i + len(key_e)].strip()
        else:
            return content

    @staticmethod
    def get_real_arr(arr):
        """
        返回删除所有空值后的arr
        """
        arr_copy = copy.deepcopy(arr)
        arr_copy = list(filter(None, arr_copy))
        while '' in arr_copy:
            arr_copy.remove('')
        return arr_copy

    @staticmethod
    def get_between(str1, start, end, include=False):
        """
        获取两个字符串中间
        :param str1:
        :param start:
        :param end:
        :param include: 默认不包含匹配值
        :return:
        """
        if start != '':
            j = str1.find(start)
            if j != -1:
                if include:
                    str1 = str1[j:].strip()
                else:
                    str1 = str1[j + len(start):].strip()
        if end != '':
            i = str1.find(end)
            if i != -1:
                if include:
                    str1 = str1[:i + len(end)].strip()
                else:
                    str1 = str1[:i].strip()

        return str1.strip()

    @staticmethod
    def list_split(items, n):
        """
        分页
        """
        return [items[i:i + n] for i in range(0, len(items), n)]

    @staticmethod
    def createDict(data, tupleVals, val):
        """
        两层字典
        :param data:
        :param tupleVals:
        :param val:
        :return:
        """
        tupNum = len(tupleVals)
        dictName = data
        for index, tupleVal in enumerate(tupleVals):
            if index == tupNum - 1:
                if tupleVal not in dictName.keys():
                    dictName[tupleVal] = [val]
                else:
                    dictName[tupleVal].append(val)
            else:
                if tupleVal not in dictName.keys():
                    dictName[tupleVal] = {}

            dictName = dictName[tupleVal]
        return data

    @staticmethod
    def pt_join(pt, num):
        """
        分割数组
        :param pt:
        :param num:
        :return:
        """
        num = [str(i) for i in num]
        return pt.join(num)

    @staticmethod
    def array_filter(arr):
        """
        去除空值
        """
        while '' in arr:
            arr.remove('')
        return arr

